/* Microsoft Reference Implementation for TPM 2.0
 *
 *  The copyright in this software is being made available under the BSD License,
 *  included below. This software may be subject to other third party and
 *  contributor rights, including patent rights, and no such rights are granted
 *  under this license.
 *
 *  Copyright (c) Microsoft Corporation
 *
 *  All rights reserved.
 *
 *  BSD License
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this list
 *  of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright notice, this
 *  list of conditions and the following disclaimer in the documentation and/or
 *  other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "Tpm.h"
#include "ClockRateAdjust_fp.h"

#if CC_ClockRateAdjust  // Conditional expansion of this file

/*
tpm2_clockrateadjust(1) - Adjusts the rate at which clock and time are updated on the TPM so one can better match real time. 
With no argument, the command is invoked but the clock rate is not altered. With an argument, the tool will adjust the clock 
and time period. The command can either increase or decrease the clock period via 3 distinct granularities: course, medium 
and fine. To specify this, the argument can be a string of 1 to 3 characters of all 's' or 'f'.
tpm2_clockrateadjust（1）-调整TPM上时钟和时间的更新速率，以便更好地匹配实时。在没有参数的情况下，会调用该命令，但不会更改时钟速率。通过参数，
该工具将调整时钟和时间段。该命令可以通过三种不同的粒度来增加或减少时钟周期：当然，中等和精细。若要指定此项，参数可以是一个由1到3个字符组成的字符串，
所有字符都为“s”或“f”。

This command adjusts the rate of advance of Clock and Time to provide a better approximation to real time.
The rateAdjust value is relative to the current rate and not the nominal rate of advance.
EXAMPLE 1 If this command had been called three times with rateAdjust = TPM_CLOCK_COARSE_SLOWER and once with rateAdjust = TPM_CLOCK_COARSE_FASTER, 
the net effect will be as if the command had been called twice with rateAdjust = TPM_CLOCK_COARSE_SLOWER.
The range of adjustment shall be sufficient to allow Clock and Time to advance at real time but no more.
If the requested adjustment would make the rate advance faster or slower than the nominal accuracy of the input frequency, the TPM shall return TPM_RC_VALUE.
EXAMPLE 2 If the frequency tolerance of the TPM's input clock is +/-10 percent, then the TPM will return TPM_RC_VALUE if the adjustment would make 
Clock run more than 10 percent faster or slower than nominal. That is, if the input oscillator were nominally 100 megahertz (MHz), then 1 millisecond (ms) 
would normally take 100,000 counts. The update Clock should be adjustable so that 1 ms is between 90,000 and 110,000 counts.
The interpretation of “fine” and “coarse” adjustments is implementation-specific.
The nominal rate of advance for Clock and Time shall be accurate to within 15 percent. That is, with no adjustment applied, Clock and Time shall be advanced 
at a rate within 15 percent of actual time.
NOTE If the adjustments are incorrect, it will be possible to make the difference between advance of Clock/Time and real time to be as much as 1.152 or ~1.33.
Changes to the current Clock update rate adjustment need not be persisted across TPM power cycles.

此命令调整时钟和时间的前进速率，以提供更好的实时近似值。
rateAdjust值是相对于当前利率而非名义预付款利率的。
示例1如果此命令在rateAdjust=TPM_CLOCK_COARSE_SLOWER的情况下调用了三次，在rateAdust=TPM.CLOCK_COARSE_FASTER的情况中调用了一次，则净效果将如同该命令在rateAdjust=TPM _ CLOCK_COARSE_SLOWER
的情况下被调用了两次一样。
调整范围应足以使时钟和时间实时前进，但不能超过。
如果所请求的调整将使速率前进比输入频率的标称精度更快或更慢，TPM应返回TPM_RC_VALUE。
示例2如果TPM输入时钟的频率公差为+/-10%，则如果调整会使时钟运行速度比标称速度快或慢10%以上，则TPM将返回TPM_RC_VALUE。也就是说，如果输入振荡器标称为100兆赫（MHz），那么1毫秒（ms）通常需要100000次计数。
更新时钟应可调，以便1毫秒在90000和110000计数之间。
对“精细”和“粗略”调整的解释是具体实施的。
时钟和时间的标称提前率应精确到15%以内。也就是说，在不进行调整的情况下，时钟和时间应以实际时间的15%以内的速率提前。
注：如果调整不正确，则可以使时钟/时间提前量与实时时间之间的差值达到1.152或~1.33。
对当前时钟更新速率调整的更改无需在TPM电源循环期间持续。
*/

/*(See part 3 specification)
// adjusts the rate of advance of Clock and Timer to provide a better
// approximation to real time.
*/
TPM_RC
TPM2_ClockRateAdjust(
    ClockRateAdjust_In  *in             // IN: input parameter list
    )
{
// Internal Data Update
    TimeSetAdjustRate(in->rateAdjust);

    return TPM_RC_SUCCESS;
}

#endif // CC_ClockRateAdjust